package com.ethereal.petmanagement.dao.impl;

import com.ethereal.petmanagement.dao.IAdminDao;
import com.ethereal.petmanagement.dto.AdminDTO;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.util.CodeMsg;
import com.ethereal.petmanagement.util.DatabaseUtil;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AdminDaoImpl implements IAdminDao {

    //按照手机号查找管理员（登录功能）
    @Override
    public ResponseDTO<AdminDTO> findAdminByPhone(AdminDTO adminDTO) {
        String sql = "select * from admin where phone = ?";
        try(
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql
        ){
            //添加参数
            pstmt.setString(1, adminDTO.getPhone());
            ResultSet rs = pstmt.executeQuery(); //查找结果
            if (rs.next()){
                adminDTO.setPhone(rs.getString("phone"));
                adminDTO.setUsername(rs.getString("username"));
                adminDTO.setId((BigInteger) rs.getObject("id"));
                adminDTO.setPassword(rs.getString("password"));
                adminDTO.setSalt(rs.getString("salt"));
            }
            return ResponseDTO.success(adminDTO);
        } catch (Exception e) {
            return ResponseDTO.errorByMsg(CodeMsg.DATABASE_ERROR);
        }
    }

    //添加管理员
    @Override
    public Integer insertAdmin(AdminDTO adminDTO) {
        String sql = "insert into admin (id, username, password, phone, salt, create_time, role_type, real_name) values (?, ?, ?, ?, ?, ?, ?, ?)";
        int rows;
        try (
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql语句
        ) {
            //添加参数
            pstmt.setObject(1, adminDTO.getId());
            pstmt.setString(2, adminDTO.getUsername());
            pstmt.setString(3, adminDTO.getPassword());
            pstmt.setString(4, adminDTO.getPhone());
            pstmt.setString(5, adminDTO.getSalt());
            pstmt.setTimestamp(6, adminDTO.getCreateTime());
            pstmt.setByte(7, adminDTO.getRoleType());
            pstmt.setString(8, adminDTO.getRealName());
            //操作数据库，将返回值给rows
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return rows;
    }

    //修改管理员信息
    @Override
    public Integer updateAdmin(AdminDTO adminDTO) {
        String sql = "update admin set username = ?, phone = ?, avatar_url = ?, update_time = ? where id = ?";
        int rows;
        try(
                Connection conn = DatabaseUtil.getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql)
        ) {
            //添加参数
            pstmt.setString(1, adminDTO.getUsername());
            pstmt.setString(2, adminDTO.getPhone());
            pstmt.setString(4, adminDTO.getAvatarUrl());
            pstmt.setTimestamp(5, adminDTO.getUpdateTime());
            pstmt.setObject(6, adminDTO.getId());
            //执行sql
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return rows;
    }
}
